package com.intexh.speedandroid.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.intexh.speedandroid.R;
import com.intexh.speedandroid.base.BaseActivity;
import com.intexh.speedandroid.net.Apis;
import com.intexh.speedandroid.net.NetworkManager;
import com.intexh.speedandroid.net.interfaces.OnRequestCallBack;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.get_code_tv)
    TextView getCodeTv;
    @BindView(R.id.mobile_code_et)
    EditText mobileCodeEt;
    @BindView(R.id.password_et)
    EditText passwordEt;
    @BindView(R.id.confirm_password_et)
    EditText confirmPasswordEt;
    @BindView(R.id.invite_code_et)
    EditText inviteCodeEt;
    @BindView(R.id.register_btn)
    Button registerBtn;
    private String mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back_iv, R.id.get_code_tv, R.id.register_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.get_code_tv:
                getCode();
                break;
            case R.id.register_btn:
                register();
                break;
        }
    }

    private void register() {
        String code = mobileCodeEt.getText().toString();
        if(TextUtils.isEmpty(code)){
            showToast("请输入验证码");
            return;
        }
        String pwd = passwordEt.getText().toString();
        if(TextUtils.isEmpty(pwd)){
            showToast("请输入密码");
            return;
        }
        if(pwd.length()<6||pwd.length()>16){
            showToast("请输入6-16位长度的密码");
            return;
        }
        String confirmPwd = confirmPasswordEt.getText().toString();
        if(!pwd.equals(confirmPwd)){
            showToast("两次输入密码不一致，请重新输入");
            return;
        }
        showProgress();
        HashMap<String,String> params = new HashMap<>() ;
        params.put("phone",mPhone) ;
        params.put("password",pwd) ;
        params.put("captcha",code) ;
        params.put("inviter_id",inviteCodeEt.getText().toString()) ;
        NetworkManager.INSTANCE.post(Apis.register, params, new OnRequestCallBack() {
            @Override
            public void onOk(String response) {
                hideProgress();
                Intent intent = new Intent();
                intent.putExtra("phone",mPhone);
                setResult(RESULT_OK,intent);
                showToast("注册成功！");
                finish();
            }

            @Override
            public void onError(int code,String errorMessage) {
                hideProgress();
                showToast(errorMessage);
            }
        });
    }

    private void getCode() {
        mPhone = phoneEt.getText().toString();
        if(TextUtils.isEmpty(mPhone)){
            showToast("请输入手机号码");
            return;
        }
        HashMap<String,String> params = new HashMap<>() ;
        params.put("phone",mPhone) ;
        params.put("type","1") ;
        showProgress();
        NetworkManager.INSTANCE.post(Apis.getMobileCode, params, new OnRequestCallBack() {
            @Override
            public void onOk(String response) {
                hideProgress();
                countDownReSend(getCodeTv,60);
            }

            @Override
            public void onError(int code,String errorMessage) {
                hideProgress();
                showToast(errorMessage);
            }
        });
    }
}
